package com.example.demo.controller;

import com.example.demo.service.AsyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@RestController
public class threadController {

    private static final Logger logger = LoggerFactory.getLogger(threadController.class);

    @Autowired
    private AsyncService asyncService;


    @RequestMapping("/")
    public String hello(){
        long start = System.currentTimeMillis();
        submit("E:\\健美upload备份\\2017");
        return (System.currentTimeMillis()-start)/1000/60+":分";
    }

    public void submit(String path){
        logger.info("start submit");
        File file = new File(path);
        if (file.exists() && file.isDirectory()) {
            File[] uploads = file.listFiles();
            if (uploads == null) {
               return;
            }
            for (File upload : uploads) {
                if (upload.isDirectory()) {
                    submit(upload.getAbsolutePath());
                }
//                        根目录
                if (upload.isFile()) {
                    asyncService.executeAsync(upload);
                }
            }
        }

        logger.info("end submit");

    }

}
